import libtcodpy as libtcod
import random
import message

ATRIBUTE_FLAMABLE_KEY = "flammable"

class attribute_flammable:

    flame_color = 0
    flame_colors = [libtcod.orange, libtcod.yellow]

    def __init__(self, owner, flame_resistance=0, is_on_fire=False, on_ignite=None):

        self.owner = owner

        self.on_ignite = on_ignite or self.on_ignite

        self.is_on_fire = is_on_fire

        # Value between 0 and 100 to indicate how resilient to flame the object is
        # 0 = ignite on spark, 100 = can not be lit on fire
        self.flame_resistance = flame_resistance
        self.flame_damage = 1


    def on_ignite(self, igniter):
        return

    def on_tick(self):
        if self.is_on_fire:
            if self.owner.attributes.has_key("fighter"):
                self.owner.attributes["fighter"].take_damage(self.flame_damage)

                # because the damage might have killed them
                if self.owner.attributes["fighter"].hp <= 0:
                    self.on_tick = None
                    self.is_on_fire = False
                    self.draw = None

        return

    def on_touch(self, toucher):

        if self.is_on_fire:
            # if the toucher has the flammable attribute then try to catch them on fire
            if toucher.attributes.has_key(ATRIBUTE_FLAMABLE_KEY):
                fire_info = toucher.attributes[ATRIBUTE_FLAMABLE_KEY]

                rand = random.random() * 100
                res = fire_info.flame_resistance

                was_on_fire = fire_info.is_on_fire

                fire_info.is_on_fire = fire_info.is_on_fire or rand > res

                if fire_info.is_on_fire and not was_on_fire:
                    message.message(toucher.name + " was set on fire by touching " + self.owner.name+".")
                    fire_info.on_ignite(self.owner)

    ##
    # draw()
    # Description: Sets the color of the object and draws it to the console
    #
    #Parameters:
    #   con - the console you want the object to be drawn on. (libtcod.console_new())
    #
    # Returns: none
    ##
    def draw(self, con, fov_map):
        if self.is_on_fire:
            # Set the color
            libtcod.console_set_default_foreground(con, self.flame_colors[self.flame_color])
            self.flame_color = (self.flame_color + 1) % len(self.flame_colors)

            # Draw the object to the console
            libtcod.console_put_char(con, self.owner.x, self.owner.y, "#", libtcod.BKGND_NONE)

